package com.magicalcoder.youyamvc.web.controller.web.goodsspeccategory;
import com.magicalcoder.youyamvc.app.goodsspeccategory.service.GoodsSpecCategoryService;
import com.magicalcoder.youyamvc.app.goodsspeccategory.service.GoodsSpecCategoryProxyService;
import com.magicalcoder.youyamvc.app.goodsspeccategory.constant.GoodsSpecCategoryConstant;
import com.magicalcoder.youyamvc.app.goodsspeccategory.util.GoodsSpecCategoryUtil;
import com.magicalcoder.youyamvc.web.controller.web.goodsspeccategory.param.GoodsSpecCategoryRequest;
import com.magicalcoder.youyamvc.web.controller.web.goodsspeccategory.param.GoodsSpecCategoryResponse;
import com.magicalcoder.youyamvc.app.model.GoodsSpecCategory;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.MapUtil;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;
import com.magicalcoder.youyamvc.core.common.dto.AjaxData;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;
import com.magicalcoder.youyamvc.core.spring.admin.AdminLoginController;
import com.magicalcoder.youyamvc.core.spring.web.WebLoginController;
import com.magicalcoder.youyamvc.web.common.BaseController;
import com.magicalcoder.youyamvc.app.userweb.util.UserWebUtil;
import com.magicalcoder.youyamvc.core.common.exception.BusinessException;

import org.springframework.format.annotation.DateTimeFormat;
import java.util.*;
import java.math.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@RequestMapping({"/user/goods_spec_category"})
@Controller
public class GoodsSpecCategoryUserController extends WebLoginController
{

    @Resource
    private GoodsSpecCategoryService goodsSpecCategoryService;
    @Resource
    private GoodsSpecCategoryProxyService goodsSpecCategoryProxyService;
    /** method:GET
    *   url:/user/goods_spec_category/list/{pageIndex}/{pageSize}?...
    *   demo:/user/goods_spec_category/list/1/20
    *   获取商品规格分类分页数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  是   @pageIndex                     (Integer)当前页码 [1,n]
    *  是   @pageSize                      (Integer)每页条数 [1,n]
    *  否   @callback                      (String)回调方法
    *  否   @orderBySqlField               (String)排序字段
    *  否   @descAsc                       (String)desc|asc
    *  否   @encode                        (String)编码 默认UTF-8
    *  否   @id                     (Long)主键 
    *  否   @name                     (String)分类名称 
    *  否   @status                     (Boolean)分类状态 [{"":"全部"},{"false":"否"},{"true":"是"}]
    *  否  like @nameFirst                     (String)分类名称 
    *  否   @statusFirst                     (Boolean)分类状态 [{"":"全部"},{"false":"否"},{"true":"是"}]
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                [{
    *              id         (Long)主键 
    *              name         (String)分类名称 
    *              status         (Boolean)分类状态 [{"":"全部"},{"false":"否"},{"true":"是"}]
    *      }]
    *   }
    */
    @RequestMapping(value={"list/{pageIndex}/{pageSize}"}, method={RequestMethod.GET})
    public void list(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,
        @RequestParam(required=false, value="orderBySqlField") String orderBySqlField,
        @RequestParam(required=false, value="descAsc") String descAsc,
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")   Long id,
        @RequestParam(required = false,value ="name")   String name,
        @RequestParam(required = false,value ="status")   Boolean status,
        @RequestParam(required = false,value ="nameFirst")                            String nameFirst ,
        @RequestParam(required = false,value ="statusFirst")                            Boolean statusFirst ,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            String orderBySqlFieldStr = ",id,name,status,";
            String orderBy = ProjectUtil.filterOrderBy(orderBySqlFieldStr,orderBySqlField,descAsc);
            if(StringUtils.isBlank(orderBy)){
                orderBy = "";
            }
            pageSize = Math.min(GoodsSpecCategoryConstant.PAGE_MAX_SIZE,pageSize);
            int idx = (pageIndex.intValue() - 1) * pageSize;
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap("orderBy", orderBy,
         "id",id,
         "name",name,
         "status",status,
                    "nameFirst",nameFirst ,
                    "statusFirst",statusFirst ,
                "start",idx,"limit", pageSize ,"userId",_userId);

            List<GoodsSpecCategory> pageList = this.goodsSpecCategoryService.getGoodsSpecCategoryList(query);
            toWebSuccessJson(callback,encode,response, coverToResponseList(pageList));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

    private List<GoodsSpecCategoryResponse> coverToResponseList(List<GoodsSpecCategory> pageList){
        if(ListUtils.isBlank(pageList)){
            return null;
        }
        return Copyer.copy(pageList,GoodsSpecCategoryResponse.class);
    }


    /** method:GET
    *   url:/user/goods_spec_category/item?...
    *   demo:/user/goods_spec_category/item
    *   获取商品规格分类详情数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  否   @callback                      (String)回调方法
    *  否   @encode                        (String)编码 默认UTF-8
     *  否  @id        (Long) 主键
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                {
    *              id         (Long)主键 
    *              name         (String)分类名称 
    *              status         (Boolean)分类状态 [{"":"全部"},{"false":"否"},{"true":"是"}]
    *      }
    *   }
    */
    @RequestMapping(value={"item"}, method={RequestMethod.GET})
    public void item(
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")Long id,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap(
                     "id",id,
                "userId",_userId
            );
            GoodsSpecCategory goodsSpecCategory = this.goodsSpecCategoryService.selectFirstGoodsSpecCategory(query);
            toWebSuccessJson(callback,encode,response, Copyer.copy(goodsSpecCategory,new GoodsSpecCategoryResponse()));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

    /** method:post
    *   url:/user/goods_spec_category/save?...
    *   demo:/user/goods_spec_category/save?...
    *   保存商品规格分类
    *   是否需要登录               是
    *是否必须         入参         注释
    * 否id (Long)主键  为空表示插入否则表示更新
    * 否name (String)分类名称  
    * 否status (Boolean)分类状态 [{"":"全部"},{"false":"否"},{"true":"是"}] 
    *
    *   返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:id 主键 Long
    *   }
    */
    @RequestMapping(value={"save"}  ,method={RequestMethod.POST})
    public void save(@ModelAttribute GoodsSpecCategoryRequest goodsSpecCategoryRequest,

        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        HttpServletRequest request,HttpServletResponse response) {
        Long _userId = UserWebUtil.userId(request);
        try{
            Long id = saveEntity(_userId,goodsSpecCategoryRequest);
            toWebSuccessJson(callback,encode,response, id);
        }catch (BusinessException e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(callback,encode,response,e.getErrorCode(), exceptionMsg);
            e.printStackTrace();
        }
        catch (Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(callback,encode,response, exceptionMsg);
            e.printStackTrace();
        }
    }

    private Long saveEntity(Long userId,GoodsSpecCategoryRequest goodsSpecCategoryRequest){
        if (goodsSpecCategoryRequest.getId() == null) {
            GoodsSpecCategory goodsSpecCategory = new GoodsSpecCategory();
            Copyer.copy(goodsSpecCategoryRequest,goodsSpecCategory);
            return this.goodsSpecCategoryService.insertGoodsSpecCategory(goodsSpecCategory);
        } else {
            GoodsSpecCategory entity = getEntity(userId,goodsSpecCategoryRequest.getId());
            if(entity!=null){
                Copyer.reflectCopyZeroNotNull(goodsSpecCategoryRequest, entity);
                this.goodsSpecCategoryService.updateGoodsSpecCategory(entity);
            }
        }
        return goodsSpecCategoryRequest.getId();
    }

    private GoodsSpecCategory getEntity(Long userId,Long id){
        return this.goodsSpecCategoryService.selectOneGoodsSpecCategoryWillThrowException(ProjectUtil.buildMap(
            "id",id));
    }

    /** method:get
    *   url:/user/goods_spec_category/delete?...
    *   demo:/user/goods_spec_category/delete?...

    *   删除商品规格分类
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  否   @callback                      (String)回调方法
    *  否   @encode                        (String)编码 默认UTF-8
    *  否  @id        (Long) 主键
    *   返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:true||false
    *   }
    */
    @RequestMapping(value={"delete"}, method={RequestMethod.GET})
    public void delete(
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")Long id,
    HttpServletRequest request,HttpServletResponse response)
    {
        try{
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap(
            "id",id,
            "userId",_userId
            );
            this.goodsSpecCategoryService.deleteGoodsSpecCategoryList(query);
            toWebSuccessJson(callback,encode,response, true);
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }



}